package com.wgo.bpot.domain.common.impl;

import com.wgo.bpot.domain.common.ServicePrivilege;
import com.wgo.bpot.domain.common.User;
import com.wgo.bpot.domain.common.UserRole;

/**
 * @version $Id: UserImpl.java 540 2008-01-09 19:42:31Z petter.eide $
 */
public class UserImpl implements User {

	private Long dbId;
	private String firstName;
	private String lastName;
    private String userName;
    private String password;
	private long passwordHashCode;
	private UserRole userRole;
	private String userRoleName;
	
    public UserImpl() {
    	super();
		password = "" ;
	}

	public Long getDbId() {
        return dbId;
    }
    
    public void setDbId(Long dbId){
        this.dbId = dbId;
    }
    
	public String getFirstName() {
		return firstName;
	}
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public String getUserName() {
		return userName;
	}
	
	public void setUserName(String userName) {
    	if ( null == userName ){
    		throw new IllegalArgumentException("Username cannot be null");
    	}
		this.userName = userName;
	}
	
	public long getPasswordHashCode() {
		return passwordHashCode;
	}
	
	public void setPasswordHashCode(long passwordHashCode) {
		this.passwordHashCode = passwordHashCode;
	}
	
	public UserRole getUserRole() {
		return userRole;
	}
    
    public void setUserRole(UserRole userRole) {
        this.userRole = userRole;
        setUserRoleName((null == userRole) ? null : userRole.getName());
    }
    
	public void setPassword(String password) {
    	if ( null == password ){
    		throw new NullPointerException("Password cannot be null");
    	}
        this.password = password;
		setPasswordHashCode(createHashCode(password));
	}
	
	public String getPassword() {
        return password;
    }

    public static final long createHashCode(String password){
		return password.hashCode();
	}
    
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((getUserName() == null) ? 0 : getUserName().hashCode());
		return result;
	}
	
	/**
	 * TODO: avoid
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final UserImpl other = (UserImpl) obj;
		if (getUserName() == null) {
			if (other.getUserName() != null)
				return false;
		} else if (!getUserName().equals(other.getUserName())) {
			return false;
		} else if (null != getDbId() && ! getDbId().equals(other.getDbId())) {
			return false;
		} else if (null == other.getDbId() && null != getDbId() ) {
			return false;
		}
		return true;
	}
	
	@Override
	public String toString() {
		return getUserName();
	}
    
    public boolean isAllowed(ServicePrivilege... servicePrivileges) {
        return (null == userRole) ? false : userRole.isAllowed(servicePrivileges);
    }
    
	public String getUserRoleName() {
		return userRoleName;
	}

	public void setUserRoleName(String userRoleName) {
		this.userRoleName = userRoleName;
	}
    
}
